/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.tukano.text;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Text format utility
 * @author utente3
 */
public class TextFormat {

    /* Shared instance*/
    private static final TextFormat INSTANCE = new TextFormat();

    /**
     * Returns a shared instance of TextFormat
     * @return the shared instance of TextFormat
     */
    public static TextFormat getInstance() {
        return INSTANCE;
    }

    private static final String DIGITS = "0123456789";

    /**
     * Checks if the given text is empty or null
     * @param text the text to check
     * @return true if text is empty or null, false otherwise
     */
    public boolean isEmpty(String text) {
        return text == null || text.isEmpty() | text.trim().isEmpty();
    }

    /**
     * Checks if the given string is not a decimal string
     * @param text the text to check
     * @return true if the text is not a decimal number
     */
    public boolean isNotDecimal(String text) {
        return isNotCurrency(text);
    }

    /**
     * Checks if the given string is not a currency (dot used as decimal
     * separator)
     * @param text the text to check
     * @return true if the text is not a currency string
     */
    public boolean isNotCurrency(String text) {
        if(isEmpty(text)) return true;
        int dotcount = 0;
        for (int i= 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if(c == '.') {
                dotcount++;
                if(dotcount > 1) return true;
            } else if(!Character.isDigit(c)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Checks if the given string can be a phone number (a numeric sequence)
     * @param number the string to check
     * @return true if the given string can express a phone number, false
     * otherwise
     */
    public boolean isPhoneNumber(String number) {
        for (int i= 0; i < number.length(); i++) {
            if(DIGITS.indexOf(number.charAt(i)) < 0) return false;
        }
        return true;
    }

    /**
     * Formats a big decimal as a currency string (d.ddd.ddd,dd)
     * @param d the big decimal to format
     * @return the currency string
     */
    public String asCurrencyString(BigDecimal d) {
        String s = d.setScale(2, RoundingMode.HALF_EVEN).toString();
        String fractional = s.substring(s.length() - 2, s.length());
        String integral = s.substring(0, s.length() - 3);
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < integral.length(); i++) {
            char n = integral.charAt(integral.length() - 1 - i);
            if(i != 0 && i % 3 == 0) {
                buffer.insert(0, ".");
            }
            buffer.insert(0, n);
        }
        return buffer.toString() + "," + fractional;
    }

    /**
     * Filters out characters that are not in the validChars sequence
     * @param text the text to filter
     * @param validChars the char sequence to accept
     * @return the filtered string. Can be empty, never null
     */
    public String filterCharsInclusive(String text, String validChars) {
        StringBuilder buffer = new StringBuilder();
        for (int i= 0; i < text.length(); i++) {
            if(validChars.contains(text.substring(i, i + 1))) {
                buffer.append(text.substring(i, i + 1));
            }
        }
        return buffer.toString();
    }

    public boolean isDecimal(String premio) {
        return !isNotDecimal(premio);
    }
}
